class RootCategoriesController < ApplicationController
  # GET /root_categories
  # GET /root_categories.xml
  def index
    @root_categories = RootCategory.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @root_categories }
    end
  end

  # GET /needcategories
  # GET /needcategories.xml
  def needcategories
	  priority = Integer(params[:priority])
		
		@root_categories = RootCategory.all.find_all do |a| 
			(a.priority == priority)
		end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :action => "needcategories.rxml"}
    end
  end
	
	
  # GET /root_categories/1
  # GET /root_categories/1.xml
  def show
    @root_category = RootCategory.find(params[:id])
		if(params[:purpose] == :inspect) 
			session[:activeRootCategory] = 0
			respond_to do |format|
				format.html # show.html.erb
				format.xml  { render :xml => @root_category }
			end
		else
		   session[:activeRootCategory] = params[:id]
			 redirect_to "/categories"
		end 
  end
	
	
  # GET /root_categories/new
  # GET /root_categories/new.xml
  def new
    @root_category = RootCategory.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @root_category }
    end
  end

  # GET /root_categories/1/edit
  def edit
    @root_category = RootCategory.find(params[:id])
  end

  # POST /root_categories
  # POST /root_categories.xml
  def create
    @root_category = RootCategory.new(params[:root_category])

    respond_to do |format|
      if @root_category.save
        format.html { redirect_to(@root_category, :notice => 'RootCategory was successfully created.') }
        format.xml  { render :xml => @root_category, :status => :created, :location => @root_category }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @root_category.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /root_categories/1
  # PUT /root_categories/1.xml
  def update
    @root_category = RootCategory.find(params[:id])

    respond_to do |format|
      if @root_category.update_attributes(params[:root_category])
        format.html { redirect_to(@root_category, :notice => 'RootCategory was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @root_category.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /root_categories/1
  # DELETE /root_categories/1.xml
  def destroy
    @root_category = RootCategory.find(params[:id])
    @root_category.destroy

    respond_to do |format|
      format.html { redirect_to(root_categories_url) }
      format.xml  { head :ok }
    end
  end
end
